# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : MakeIDNum.py
# Time       ：2023-08-12 10:10
# Author     ：张铨
# version    ：
# Description：
"""
from random import random
from faker import Faker

'''
根据行政地区码、日期和性别生成
'''


def generate_id_number(address_code, birth_date, gender):
    # 前 6 位表示地址码，这里使用随机数代替
    # address_code = '{:0>6}'.format(random.randint(1, 999999))
    # address_code = '130921'

    # 中间 8 位表示出生日期，格式为 YYYYMMDD
    birth_code = birth_date.strftime('%Y%m%d')

    # 接下来 3 位表示顺序码，奇数表示男性，偶数表示女性
    if gender.lower() == 'male':
        sequence_code = '{:0>3}'.format(random.randrange(1, 999, 2))
    elif gender.lower() == 'female':
        sequence_code = '{:0>3}'.format(random.randrange(0, 999, 2))
    else:
        raise ValueError('Invalid gender')

    # 组合前 17 位
    id_number = address_code + birth_code + sequence_code

    # 计算校验码
    weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    check_codes = '10X98765432'
    check_sum = sum(int(num) * weight for num, weight in zip(id_number, weights))
    check_code = check_codes[check_sum % 11]

    return id_number + check_code

'''
模拟相关信息
'''
faker = Faker(locale='zh_CN')
def randomIDNum():

    identify_id = faker.ssn()
    return identify_id
def randomName():
    makeName = faker.name_female()
    return  makeName
'''
生成公司名称
'''
def randomCompanyName():
    FakeCompany = faker.company()
    return FakeCompany



'''
模拟社会统一信用代码
'''
import random

# 统一社会信用代码最后一位：代码字符集
import time

check_dict = {
    "0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9,
    "A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15, "G": 16, "H": 17, "J": 18, "K": 19, "L": 20, "M": 21,
    "N": 22, "P": 23, "Q": 24, "R": 25, "T": 26, "U": 27, "W": 28, "X": 29, "Y": 30
}
dict_check = {value: key for key, value in check_dict.items()}
# 组织机构代码 9位
def create_organization():
    weight_code = [3, 7, 9, 10, 5, 8, 4, 2]  # Wi 代表第i位上的加权因子=pow(3,i-1)%31
    org_code = []  # 组织机构代码列表
    sum = 0
    for i in range(8):
        org_code.append(dict_check[random.randint(0, 30)])  # 前八位本体代码：0~9 + A~Z 31个
        sum = sum + check_dict[org_code[i]] * weight_code[i]
    C9 = 11 - sum % 11  # 代表校验码：11-MOD（∑Ci(i=1→8)×Wi,11）-->前8位加权后与11取余，然后用11减
    if C9 == 10:
        last_code = 'X'
    elif C9 == 11:
        last_code = '0'
    else:
        last_code = str(C9)

    code = ''.join(org_code) + '-' + last_code  # 组织机构代码
    # print(code)
    return (code)

# 统一社会信用代码 18位 默认为中国机构
def create_social_credit(area_code = '100000'):
    manage_code = [9]  # 登记管理部门代码：9-工商
    type_code = [1, 2, 3, 9]  # 9-1-企业，9-2-个体工商户，9-3-农民专业合作社，9-9-其他
    area_code = area_code  # 登记管理机关行政区划码：100000-国家用
    org_code = create_organization().replace('-', '')  # 组织机构代码
    sum = 0
    weight_code = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28]  # Wi 代表第i位上的加权因子=pow(3,i-1)%31
    code = str(random.choice(manage_code)) + str(random.choice(type_code)) + area_code + org_code
    for i in range(17):
        sum = sum + check_dict[code[i:i + 1]] * weight_code[i]
    try:
        C18 = dict_check[31 - sum % 31]
        social_code = code + C18
        print(social_code)
    except Exception as e:
        return " "
    return social_code



# 组织机构代码 9位
def create_organization():
    weight_code = [3, 7, 9, 10, 5, 8, 4, 2]  # Wi 代表第i位上的加权因子=pow(3,i-1)%31
    org_code = []  # 组织机构代码列表
    sum = 0
    for i in range(8):
        org_code.append(dict_check[random.randint(0, 30)])  # 前八位本体代码：0~9 + A~Z 31个
        sum = sum + check_dict[org_code[i]] * weight_code[i]
    C9 = 11 - sum % 11  # 代表校验码：11-MOD（∑Ci(i=1→8)×Wi,11）-->前8位加权后与11取余，然后用11减
    if C9 == 10:
        last_code = 'X'
    elif C9 == 11:
        last_code = '0'
    else:
        last_code = str(C9)

    code = ''.join(org_code) + '-' + last_code  # 组织机构代码
    # print(code)
    return (code)



'''
指定信息生成个人身份证号
'''
def generate_id_number(are_id,birth_date, gender):
    # 前 6 位表示地址码，这里使用随机数代替
    address_code = are_id
    # 中间 8 位表示出生日期，格式为 YYYYMMDD
    birth_code = birth_date.strftime('%Y%m%d')
    # 接下来 3 位表示顺序码，奇数表示男性，偶数表示女性
    if gender.lower() == 'male':
        sequence_code = '{:0>3}'.format(random.randrange(1, 999, 2))
    elif gender.lower() == 'female':
        sequence_code = '{:0>3}'.format(random.randrange(0, 999, 2))
    else:
        raise ValueError('Invalid gender')

    # 组合前 17 位
    id_number = address_code + birth_code + sequence_code

    # 计算校验码
    weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    check_codes = '10X98765432'
    check_sum = sum(int(num) * weight for num, weight in zip(id_number, weights))
    check_code = check_codes[check_sum % 11]

    return id_number + check_code





if __name__ == '__main__':
    id = randomIDNum()
    print(id)
